import walf
from getFuncton import get_train
import pandas as pd
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import os
import math

data=pd.read_csv("ratings.csv",sep="\s+")
num=20
loss=[]


user_matrix = np.random.rand(data['userId'].nunique(), 5)
item_matrix = np.random.rand(data['movieId'].nunique(),5)

for i in range(data['userId'].nunique()):
    user_matrix[i] = [np.random.random() / math.sqrt(5) for x in range(5)]
for i in range(data['movieId'].nunique()):
    item_matrix[i] = [np.random.random() / math.sqrt(5) for x in range(5)]



for i in range(num):
    all = get_train(data)
    train = all[0]
    test = all[1]

    user_matrix,item_matrix,error=walf.walf_iter(train,test,0.2,user_matrix,item_matrix)
    loss.append(error)
    # print(user_matrix)
    # print(error)
plt.plot(np.arange(num),loss)
plt.savefig('iter.png')
plt.show()